/*---------------------------------------------------------
 * Copyright (C) Microsoft Corporation. All rights reserved.
 *--------------------------------------------------------*/

/** To demonstrate code actions associated with Diagnostics problems, this file provides a mock diagnostics entries. */

import * as vscode from 'vscode';
import { CommonUnit } from './lib/commonUnit';

/** Code that is used to associate diagnostic entries with code actions. */
export const RENAME_MENTION = 'PascalCase rename sidney';

/**
 * highlight the world 
 * @param doc text document to analyze
 * @param curDiagnostics diagnostic collection
 */
export function refreshDiagnosticsRename(doc: vscode.TextDocument, curDiagnostics: vscode.DiagnosticCollection): void {
    const diagnostics: vscode.Diagnostic[] = [];

    for (let lineIndex = 0; lineIndex < doc.lineCount; lineIndex++) {
        const lineOfText = doc.lineAt(lineIndex);
        const lineString = lineOfText.text + ' ';

        let count = 0;
        let offset = 0;
        let startIndex = 0;
        let endIndex = 0;
        let isMatchWord = false;
        for (let i = 0; i < lineString.length; i++) {
            let curChar = lineString[i];
            if (!isMatchWord) {
                if (CommonUnit.isWordChar(curChar, true)) {
                    isMatchWord = true;
                    startIndex = i;
                }
            } else {
                if (!CommonUnit.isWordChar(curChar)) {
                    isMatchWord = false;
                    endIndex = i;
                    let curWord = lineString.substring(startIndex, endIndex);

                    if (CommonUnit.isPascalCase(curWord)) {
                        let tempRange = new vscode.Range(lineIndex, startIndex, lineIndex, endIndex);
                        diagnostics.push(createPascalCaseWordDiagnostic(tempRange));
                    }
                }
            }
        }
    }

    curDiagnostics.set(doc.uri, diagnostics);
}

function createPascalCaseWordDiagnostic(range: vscode.Range) {
    const diagnostic = new vscode.Diagnostic(range, "PascalCase Word, do you want to rename?",
        vscode.DiagnosticSeverity.Information);
    diagnostic.code = RENAME_MENTION;
    return diagnostic;
}

export function subscribeToDocumentChangesRename(context: vscode.ExtensionContext, emojiDiagnostics: vscode.DiagnosticCollection): void {
    if (vscode.window.activeTextEditor) {
        refreshDiagnosticsRename(vscode.window.activeTextEditor.document, emojiDiagnostics);
    }
    context.subscriptions.push(
        vscode.window.onDidChangeActiveTextEditor(editor => {
            if (editor) {
                refreshDiagnosticsRename(editor.document, emojiDiagnostics);
            }
        })
    );

    context.subscriptions.push(
        vscode.workspace.onDidChangeTextDocument(e => refreshDiagnosticsRename(e.document, emojiDiagnostics))
    );

    context.subscriptions.push(
        vscode.workspace.onDidCloseTextDocument(doc => emojiDiagnostics.delete(doc.uri))
    );

}
